<?php

namespace App\Http\Controllers\Home;

use App\Http\Model\Article;
use App\Http\Model\Category;
use Illuminate\Support\Facades\DB;

class IndexController extends CommonController
{
    //首页方法
    public function index()
    {
        //dd(\Config::get('web.web_titles', 'UTC'));
        $field = ['art_id','art_title','art_thumb','art_description'];
        //1.博文推荐查看次数最高的6篇
        $recommend = Article::orderBy('art_view', 'desc')->take(6)->get($field);
        //dd($recommend);
        //2.文章推荐 最新发布的顺序排序 (分页每页5篇)
        $list = Article::orderBy('art_time','desc')->paginate(5, $field);
        //dd($list);

        /*//3.4.5 最新的文章8篇 放到CommonController控制器中公共变量中引入
        $new = Article::orderBy('art_time','desc')->take(8)->get($field);
        $hot = Article::orderBy('art_view', 'desc')->take(5)->get(['art_id','art_title']);*/

        $data = ['recommend' => $recommend, 'list' => $list];

        return view('home.index')->with($data);
    }

    public function clist($id)
    {
        //1.取出栏目数据
        $category = Category::find($id);

        $field = ['art_id','art_title','art_description','art_thumb','art_time','art_editor','art_view','cate_id','art_order'];
        //2.取出当前栏目及子栏目的所有文章 5篇一页 方法一：查询所有子栏目，再使用whereIn 查询
        /*$cate_son = Category::where('cate_pid',$id)->get(); //遗留问题1：栏目查询一次遍历操作
        $inArray[] = intval($id);
        foreach ($cate_son as $v) {
            $inArray[] = $v['cate_id'];
        }
        $list = Article::whereIn('cate_id', $inArray)->paginate(5);*/

        //2.取出当前栏目及子栏目的所有文章 5篇一页 方法二：关联模型 -- 实现select * from a where pid in (select id from b)效果
        //Article 添加 category 映射
        $list = Article::whereHas('article_category', function ($art_cate) use($id) {
            $art_cate->where('cate_id',$id)->orWhere('cate_pid',$id);
        })->paginate(5, $field);

        //2.方法三：原生sql语句 未完成分页效果
        //$prefix = env('DB_PREFIX'); //表前缀
        //$sql = 'SELECT art_id,art_title,art_tag,art_description,art_thumb,art_time,art_editor,cate_id FROM blog_article WHERE cate_id IN ( SELECT cate_id FROM blog_category WHERE cate_id = ? OR cate_pid = ?) ORDER BY art_order DESC;';
        //$slist = DB::select($sql, [$id,$id]);
        //dd($slist);

        //$data = Category::where('cate_id', $id)->orWhere('cate_pid',$id)->cate_article();
        //$data = Article::cate_article($id);

        //$data = ['category' => $category, 'list' => $list, 'cate_son' => $cate_son];
        $data = ['category' => $category, 'list' => $list];
        return view('home.list')->with($data);
    }

    public function details($art_id)
    {
        //1.取出文章 关联栏目 一对一模型
        //$article = Article::find($art_id);
        //$filed = ['art_id','art_title','art_tag', 'art_description','art_time','art_editor','art_view','cate_id','art_order'];//需要查询的字段
        $data = Article::with('ext_cate')->find($art_id);

        //2.上一篇，下一篇查询
        $field = ['art_id','art_title'];
        $data['pre_article'] = Article::where('art_id','<',$art_id)->orderBy('art_id','asc')->first($field);

        $data['next_article'] = Article::where('art_id','>',$art_id)->orderBy('art_id','desc')->first($field);

        //3.查询相同栏目的6篇文章
        $data['like_cate'] = Article::where('cate_id', $data->cate_id)->take(6)->get($field);

        $data = ['data' => $data];
        return view('home.new')->with($data);
    }

}
